package com.fivium.bpd.component;

import java.util.Vector;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.Element;

public class BusinessProcessDefinition
    implements DocumentSerializable
{
  public static final String ELEMENT_BUSINESS_PROCESS_DEFINITION  = "BUSINESS_PROCESS_DEFINITION";
  public static final String ELEMENT_SHORT_NAME                   = "SHORT_NAME";
  public static final String ELEMENT_FULL_NAME                    = "FULL_NAME";
  public static final String ELEMENT_DESCRIPTION                  = "DESCRIPTION";
  
  private String shortName_;
  private String fullName_;
  private String description_;
  
  private BusinessProcessDefinition businessProcessDefinition_;  
  private Vector<Assignment> assignmentList_            = new Vector<Assignment>();
  private Vector<QueryDefinition> queryDefinitionList_  = new Vector<QueryDefinition>();
  private Vector<EntryStage> entryStageList_            = new Vector<EntryStage>();
  private Vector<SyncStage> syncStageList_              = new Vector<SyncStage>();
  private Vector<StandardStage> standardStageList_      = new Vector<StandardStage>();
  private Vector<ConditionStage> conditionStageList_    = new Vector<ConditionStage>();
  private Vector<ExitStage> exitStageList_              = new Vector<ExitStage>();
  
  public String getShortName()
  {
    return shortName_;
  }

  public void setShortName(String shortName)
  {
    shortName_ = shortName;
  }

  public String getFullName()
  {
    return fullName_;
  }

  public void setFullName(String fullName)
  {
    fullName_ = fullName;
  }

  public String getDescription()
  {
    return description_;
  }

  public void setDescription(String description)
  {
    description_ = description;
  }
  
  public void setBusinessProcessDefinition(BusinessProcessDefinition businessProcessDefinition)
  {
    businessProcessDefinition_ = businessProcessDefinition;
  }
  
  public BusinessProcessDefinition getBusinessProcessDefinition()
  {
    return businessProcessDefinition_;
  }
  
  public void addAssignment(Assignment assignment)
  {
    assignmentList_.add(assignment);
    //Collections.sort(assignmentList_);
  }
  
  public Vector<Assignment> getAssignmentList()
  {
    return assignmentList_;
  }
  
  public void addQueryDefinition(QueryDefinition queryDefinition)
  {
    queryDefinitionList_.add(queryDefinition);
    //Collections.sort(queryDefinitionList_);
  }
  
  public Vector<QueryDefinition> getQueryDefinitionList()
  {
    return queryDefinitionList_;
  }
  
  public void addEntryStage(EntryStage entryStage)
  {
    entryStageList_.add(entryStage);
  }
  
  public Vector<EntryStage> getEntryStageList()
  {
    return entryStageList_;
  }
  
  public void addSyncStage(SyncStage syncStage)
  {
    syncStageList_.add(syncStage);
  }
  
  public Vector<SyncStage> getSyncStageList()
  {
    return syncStageList_;
  }
  
  public void addStandardStage(StandardStage standardStage)
  {
    standardStageList_.add(standardStage);
  }
  
  public Vector<StandardStage> getStandardStageList()
  {
    return standardStageList_;
  }
  
  public void addConditionStage(ConditionStage conditionStage)
  {
    conditionStageList_.add(conditionStage);
  }
  
  public Vector<ConditionStage> getConditionStageList()
  {
    return conditionStageList_;
  }
  
  public void addExitStage(ExitStage exitStage)
  {
    exitStageList_.add(exitStage);
  }
  
  public Vector<ExitStage> getExitStageList()
  {
    return exitStageList_;
  }
  
  @Override
  public Document serializeToDocument()
  {
    try {
      DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
      DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
      Document bpdXml = documentBuilder.newDocument();
      
      Element rootElement = bpdXml.createElement(ELEMENT_BUSINESS_PROCESS_DEFINITION);
      bpdXml.appendChild(rootElement);
      
      Element shortNameElement = bpdXml.createElement(ELEMENT_SHORT_NAME);
      shortNameElement.setTextContent(shortName_);
      rootElement.appendChild(shortNameElement);
      
      Element fullNameElement = bpdXml.createElement(ELEMENT_FULL_NAME);
      fullNameElement.setTextContent(fullName_);
      rootElement.appendChild(fullNameElement);
      
      Element descriptionElement = bpdXml.createElement(ELEMENT_DESCRIPTION);
      descriptionElement.setTextContent(description_);
      rootElement.appendChild(descriptionElement);
      
      return bpdXml;
    }
    catch (ParserConfigurationException e) {
      e.printStackTrace();
    }

    return null;
  }
}
